<template>
  <div class='edit-item-dialog'>
    <el-dialog :visible.sync="visible"
               width="660px"
               :title="(dialogStatus === 'create' ? '新增' : '编辑') + '检查模块'"
               :close-on-click-modal="false"
               @close="dialogClose">
      <el-form :model="editModuleForm"
               :rules="formRules"
               ref="moduleForm"
               label-width="110px">
        <el-form-item label="检查模块名称"
                      prop="module_name">
          <el-input v-model="editModuleForm.module_name"
                    placeholder="请输入检查模块名称">
          </el-input>
        </el-form-item>

      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button @click="dialogClose">取 消</el-button>
        <el-button @click="editModule"
                   type="primary">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { CommonCheckApi } from '@/services/SystemSetting'
export default {
  data () {
    return {
      visible: false,
      editModuleForm: {
        module_name: '',

      },
      resetForm: {
        module_name: '',

      },
      formRules: {
        module_name: [
          { required: true, message: '请输入检查模块名称', trigger: 'blur' }
        ],
      }
    }
  },
  props: {
    dialogVisible: {
      type: Boolean
    },
    dialogStatus: {
      type: String,
      default: "create"
    },
    dialogForm: {
      type: Object
    },
    callback: {
      type: Function
    }
  },
  watch: {
    dialogVisible (newval) {
      this.visible = newval;
    },
    dialogForm: {
      handler (newval, oldval) {

        let temp = JSON.parse(JSON.stringify(newval));
        let reset = JSON.parse(JSON.stringify(this.resetForm))
        this.editModuleForm = Object.assign(reset, temp)
      },
      deep: true
    }
  },
  methods: {
    dialogClose () {
      this.$refs.moduleForm.resetFields();
      this.$emit("update:dialogVisible", false);
    },
    editModule () {
      this.$refs.moduleForm.validate(valid => {
        if (valid) {
          if (this.dialogStatus === "create") {
            this.createItem();
          } else {
            this.updateItem();
          }
        }
      });
    },
    createItem () {
      CommonCheckApi.createCheckModule({
        data: this.editModuleForm
      }).then(res => {
        if (res.code === 0) {
          this.$message.success("新增检查模块成功");
          this.dialogClose();
          this.$emit("callback", "");
        }
      })
    },
    updateItem () {
      CommonCheckApi.updateCheckModule({
        data: this.editModuleForm
      }).then(res => {
        if (res.code === 0) {
          this.$message.success("修改检查模块成功");
          this.dialogClose();
          this.$emit("callback", "");
        }
      })
    }
  },
  created () {
    // this.editModuleForm = JSON.parse(JSON.stringify(this.dialogForm));
  },
  mounted () {
  }
}
</script>
<style scoped lang='scss'>
</style>